﻿using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.AccountingServiceReference;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Dictionary;

namespace DxMvcApplication.Repositories.Implementations.Dictionary
{
    public class FixedAssetRepository : AccountingRepositoryBase, IFixedAssetRepository
    {
        public int GetPageSize()
        {
            var request = new FixedAssetRequest().Prepare();
            request.LoadOptions = new[] { "GetPageSize" };
            request.Options = OptionType.IsPaging;
            var response = AccountingServiceClient.GetFixedAssets(request);
            Correlate(request, response);
            if (response.Acknowledge != AcknowledgeType.Success) throw new ApplicationException(response.Message);
            return response.TotalPage;
        }

        public List<FixedAssetDto> GetList(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        public List<FixedAssetDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new FixedAssetRequest().Prepare();
            request.LoadOptions = new[] { "FixedAssets" };
            request.Options = OptionType.IsPaging | OptionType.IsUnit;
            request.CurrentPage = currentPage;
            var response = AccountingServiceClient.GetFixedAssets(request);
            Correlate(request, response);
            if (response.Acknowledge != AcknowledgeType.Success) throw new ApplicationException(response.Message);
            return response.FixedAssets.ToList();
        }

        public FixedAssetDto Get(int id)
        {
            var request = new FixedAssetRequest().Prepare();
            request.LoadOptions = new[] { "FixedAsset" };
            request.Criteria = new FixedAssetCriteria { FixedAssetId = id };
            var response = AccountingServiceClient.GetFixedAssets(request);
            Correlate(request, response);
            if (response.Acknowledge != AcknowledgeType.Success) throw new ApplicationException(response.Message);
            return response.FixedAsset;
        }

        public string Insert(FixedAssetDto fixedAsset)
        {
            var request = new FixedAssetRequest().Prepare();
            request.Action = "Create";
            request.FixedAsset = fixedAsset;
            var response = AccountingServiceClient.SetFixedAssets(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.Message;
        }

        public string Update(FixedAssetDto fixedAsset)
        {
            var request = new FixedAssetRequest().Prepare();
            request.Action = "Update";
            request.FixedAsset = fixedAsset;
            var response = AccountingServiceClient.SetFixedAssets(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.Message;
        }

        public string Delete(IList<int> idList)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the specified fixedAsset identifier.
        /// </summary>
        /// <param name="fixedAssetId">The fixedAsset identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Delete(int fixedAssetId)
        {
            var request = new FixedAssetRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new FixedAssetCriteria { FixedAssetId = fixedAssetId };
            var response = AccountingServiceClient.SetFixedAssets(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.Message;
        }
    }
}